package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.util.RequireLogin;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class VideoAuthController {
    @Autowired
    private ILoginInfoService loginInfoService;
    @Autowired
    private IOrderTimeService orderTimeService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IVideoAuthService videoAuthService;

    @RequestMapping("/videoAuditOrder")
    @RequireLogin
    public String videoAuthOrderPage(Model model){
        UserInfo current = userInfoService.getCurrent();
        if (current.getHasVideoAuth()) {
            model.addAttribute("auditing", false);
            return "videoOrder_result";
        } else {
            if (current.getVideoAuthId() == null) { //没有审核或审核不通过,跳转到未审核界面
                model.addAttribute("auditors", loginInfoService.queryAuditorList());
                List<Date> orderDates = new ArrayList<Date>();
                Date now = new Date();
                orderDates.add(now);
                orderDates.add(DateUtils.addDays(now, 1));
                orderDates.add(DateUtils.addDays(now, 2));
                orderDates.add(DateUtils.addDays(now, 3));
                orderDates.add(DateUtils.addDays(now, 4));
                model.addAttribute("orderDates", orderDates);
                model.addAttribute("orderTimes", orderTimeService.selectAll());
                return "videoOrder";
            } else {  //待审核界面
                model.addAttribute("auditing", true);
                model.addAttribute("videoAuth", videoAuthService.get(current.getVideoAuthId()));
                return "videoOrder_result";
            }
        }
    }

    @RequestMapping("/videoAuth_apply")
    @ResponseBody
    public JSONResult videoAuthApply(Long timeid, String orderDate, Long auditorId){
        JSONResult result = null;
        try {
            videoAuthService.videoAuthApply(timeid, orderDate, auditorId);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }
}
